#include "head.h"

int main() {
    // 获取命令
    char temp[1024] = {0};
    read(STDIN_FILENO, temp, sizeof(temp));
    temp[strlen(temp) - 1] = 0;

    // 分离命令
    char cmd[1024] = {0};
    char args[1024] = {0};
    char *p = temp;
    while (*p == ' ' || *p == '\t') {
        ++p;
    }

    char *saveptr = NULL;
    // 拿到命令(cd,ll,ls...)
    char *token = strtok_r(p, " \t\n", &saveptr);

    if (token) {
        strncpy(cmd, token, sizeof(cmd) - 1);
        char *arg_ptr = args;
        while ((token = strtok_r(NULL, " \t\n", &saveptr))) {
            int len = strlen(token);
            if (arg_ptr + len < args + sizeof(args)) {
                memcpy(arg_ptr, token, len);
                arg_ptr += len;
            }
        }
        *arg_ptr = '\0';
    } else {
        cmd[0] = 0;
        args[0] = 0;
    }
    printf("  Cmd: [%s], Arg: [%s]\n", cmd, args);
}
